package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class LabelService {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    public void save(Label label) {
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    public void update(Label label) {
        labelDao.save(label);
    }

    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }
    public List<Label> findSearch(Label label){
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param  //根对象
             * @param criteriaQuery //封装的查询关键字 order by
             * @param criteriaBuilder //封装条件对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                //new 一个list集合存放所有条件
                List<Predicate> list=new ArrayList<>();
               if(label.getLabelname()!=null&&"".equals(label.getLabelname())){
                   //where labelname like %aa%
                   Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                   list.add(predicate);
                }
                if(label.getState()!=null&&"".equals(label.getState())){
                    //where labelname like %aa%
                    Predicate predicate = criteriaBuilder.equal(root.get("state").as(String.class),  label.getState() );
                    list.add(predicate);
                }
                Predicate[] predicate=new Predicate[list.size()];
               list.toArray(predicate);
                return criteriaBuilder.and(predicate); //where a="" and b=""
            }
        });

    }


    public Page<Label> pageQuery(Label label, int page, int size) {
        Pageable pageable= PageRequest.of(page-1,size);
        return  labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param  //根对象
             * @param criteriaQuery //封装的查询关键字 order by
             * @param criteriaBuilder //封装条件对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                //new 一个list集合存放所有条件
                List<Predicate> list=new ArrayList<>();
                if(label.getLabelname()!=null&&"".equals(label.getLabelname())){
                    //where labelname like %aa%
                    Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate);
                }
                if(label.getState()!=null&&"".equals(label.getState())){
                    //where labelname like %aa%
                    Predicate predicate = criteriaBuilder.equal(root.get("state").as(String.class),  label.getState() );
                    list.add(predicate);
                }
                Predicate[] predicate=new Predicate[list.size()];
                list.toArray(predicate);
                return criteriaBuilder.and(predicate); //where a="" and b=""
            }
        },pageable);
    }
}
